angular.module('Controller')
.controller 'TecDocsCtrl', ($scope, $state, menuManage, commonUtil, userData, $localStorage, commonConstant, wfAlert, $http) ->
    console.log "TecDocsCtrl"
    $scope.$storage = $localStorage
    $scope.currentPage = 0
    $scope.contentList =[]
    $('html, body').animate({
        scrollTop: $("#header").offset().top
    }, 100);
    $scope.hoverFunc = (item)->
        item.hover = true
    $scope.noneFunc = (item)->
        item.hover = false
    pageFunc = (page,size,count)->
        console.log "pageFunc"
        $scope.currentPage = page
        getNews page-1
    getNews = (page,pagingFlag,flag)->
        requestParams = {
            page:page
            size:9
            type:$scope.currentId
            language:$localStorage['language']
            module:2
        }
        $http.get("/wainNews",params:requestParams)
        .success (resp)->
            if resp.code==0
                if !flag
                    console.log '#news-activity-paging',pagingFlag
                    $scope.contentList = resp.message.content
                    if pagingFlag
                        $('#news-activity-paging').Paging {current:$scope.currentPage+1,pagesize:9,count:resp.message.totalElements,callback:pageFunc}
                        console.log "paging done"
                else
                    $scope.contentList = $scope.contentList.concat resp.message.content
                    $scope.currentPage++
                    console.log $scope.contentList
                if resp.message.totalElements == $scope.contentList.length
                    $scope.canload = false
                else
                    $scope.canload = true
            else
                wfAlert.toastError(resp.message)
        .error (err)->
            wfAlert.toastError(err.message)

    _.extend $scope,
        subMenus: [
            {
                name: "元器件选型"
                type: "subPage"
                enName: "Component Selection"
                route: "tec-docs"
                id: 1
            }
            {
                name: "应用笔记"
                type: "subPage"
                enName: "Application Notes"
                route: "tec-docs"
                id: 2
            }
            {
                name: "设计指南"
                type: "subPage"
                enName: "Design Notes"
                route: "tec-docs"
                id: 3
            }
            {
                name: "解决方案"
                type: "subPage"
                enName: "Solution"
                route: "tec-docs"
                id: 4
            }
            {
                name: "焊接知识"
                type: "subPage"
                enName: "Software"
                route: "product-info"
                id: 5
            }
            {
                name: "其他"
                type: "subPage"
                enName: "Others"
                route: "product-info"
                id: 6
            }
        ]
        currentId:$state.params.childId
        getName:->
            obj = _.find $scope.subMenus,(item)->
                ""+item.id == ""+$state.params.childId
            obj
        clickSubMenu:(id)->
            $state.go('tec-docs',{childId:id})
        goTechnicalDocsDetail:(item)->
            $localStorage['currentNews'] = item
            $state.go "tec-docs-detail",{childId:$scope.currentId}
        loadMore:->
            if $scope.currentPage == 0
                $scope.currentPage =1
            getNews $scope.currentPage,false,true

    getNews $scope.currentPage,true,$localStorage['isMobile']
    $scope.$on 'language.changed', ->
        getNews $scope.currentPage,false
    dynamicNavWid=()->
        wordWid=0
        _.each $('.nav-item-tag'),(tagElem)->
            wordWid+=tagElem.scrollWidth
        navWid=$('.public-pc-nav').parent().css('width').replace('px','')
        sumWid=parseInt(navWid)-32
        marginWid=((sumWid-wordWid)/($('.nav-item-tag').length-1)).toFixed(4)
        marginWid=8 if marginWid<8
        $(".nav-item-tag").css('margin-right',marginWid+'px')
    _.defer(()->
        dynamicNavWid()
    )


